package com.yanggu.mapreduce.serializable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2019/12/14 22:11
 * @description:
 */
public class FlowCountReducer extends Reducer<Text, FlowBean, Text, FlowBean> {

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {

        //1. 累加求和
        int upFlowSum = 0;
        int downFlowSum = 0;

        for (FlowBean value : values) {
            upFlowSum += value.getUpFlow();
            downFlowSum += value.getDownFlow();
        }

        //2. 封装对象
        FlowBean flowBean = new FlowBean(upFlowSum, downFlowSum);

        //3. 写出
        context.write(key, flowBean);

    }
}
